package com.wxyzz.canteenadmin.shop.desk.controller;

import com.github.pagehelper.PageInfo;
import com.wxyzz.canteenadmin.shop.desk.VO.DeskVO;
import com.wxyzz.canteenadmin.shop.desk.param.DeskAddParam;
import com.wxyzz.canteenadmin.shop.desk.param.DeskPageParam;
import com.wxyzz.canteenadmin.shop.desk.service.DeskService;
import com.wxyzz.canteenadmin.shop.employee.VO.EmployeeVO;
import com.wxyzz.canteenadmin.shop.employee.param.EmployeePageParam;
import com.wxyzz.canteencommon.common.R;
import com.wxyzz.canteencommon.entity.shop.desk.Desk;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/10/12
 * @Solgne 从想做到能做
 * @Desc
 */
@RestController
@RequestMapping("/shop/desk")
@CrossOrigin
public class DeskController {
    @Autowired
    private DeskService deskService;

    @GetMapping("/page")
    public R page (DeskPageParam deskPageParam) {
        PageInfo<DeskVO> deskVOPageInfo = deskService.page(deskPageParam);
        return R.ok(deskVOPageInfo);
    }

    @GetMapping("/deskInfo")
    public R deskInfo (Integer id) {
        Desk desk = deskService.findById(id);
        return R.ok(desk);
    }

    @GetMapping("/del")
    public R del (Integer id) {
        deskService.delById(id);
        return R.ok();
    }

    @GetMapping("/delBatch")
    public R delBatch (@RequestParam("ids") List<Integer> ids) {
        deskService.delBatch(ids);
        return R.ok();
    }

    @PostMapping("/add")
    public R add (@RequestBody DeskAddParam deskAddParam){
        System.out.println(deskAddParam);
        deskService.add(deskAddParam);
        return R.ok();
    }

    @PostMapping("/update")
    public R update (@RequestBody Desk desk){
        deskService.update(desk);
        return R.ok();
    }

    @PostMapping("/upload")
    public R upload (MultipartFile file, HttpServletRequest req) throws IOException {
        //获取要上传的路径
        String realPath = req.getServletContext().getRealPath("/upload");
        //创建该路径
        File file1 = new File(realPath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //获取源文件的名字
        String originalFilename = file.getOriginalFilename();
        //获取源文件后缀
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i + 1);
        //获取当前时间戳当上传后的的新文件的名字
        long prefix = System.currentTimeMillis();
        //将名字和后缀拼起来就是新文件的源文件名
        String newFileName = prefix + "." + suffix;
        //将新文件的对象创建出来
        File newFile = new File(file1, newFileName);
        //将被上传的文件写入到新文件中
        FileUtils.writeByteArrayToFile(newFile, file.getBytes());
        //新文件的路径
        String path = "http://121.43.41.11:8080/upload/" + newFileName;

        System.out.println(path);

        return R.ok(path);
    }
}
